//
//  MoviesViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MoviesViewController.h"
#import "MovieDetailViewController.h"
#import "MovieListCell.h"
#import "Movie.h"
#import "AppTools.h"
#import "MBProgressHUD.h"
#import "DataBaseSingleng.h"

@interface MoviesViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *movieArray;
@property (nonatomic, retain) MBProgressHUD *hub;


@end

@implementation MoviesViewController

- (void)dealloc
{
    [_hub release];
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//    self.myTableView.backgroundColor = [UIColor cyanColor];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    //设置标题
    self.navigationItem.title = @"电影";
    
    //设置导航栏右侧按钮
    UIImage *LieImage = [UIImage imageNamed:@"btn_nav_collection@2x.png"];
    LieImage = [LieImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:LieImage style:UIBarButtonItemStylePlain target:self action:@selector(rightButton:)];
    
    //注册cell
    [self.myTableView registerClass:[MovieListCell class] forCellReuseIdentifier:@"movie"];
    
    //初始化数组
    self.movieArray = [NSMutableArray array];

    
    //数据缓存
    self.movieArray = [NSMutableArray arrayWithArray:[[DataBaseSingleng shareDataBase]selectMovie]];
    
    //请求数据
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";

    //让菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"玩命加载ing......";
    [self.view addSubview:self.hub];
    
   [AppTools getDataWithUrlStr:urlStr andBlock:^(id result) {
       
       NSMutableDictionary *dic = result;
       NSString *Code = [dic objectForKey:@"error_code"];
       NSString *reason = [dic objectForKey:@"reason"];
       NSMutableArray *array = [dic objectForKey:@"result"];
       //判断是否请求到数据
       if ([reason isEqualToString:@"success"]) {
           [[DataBaseSingleng shareDataBase] deleteMovieTable];
           [[DataBaseSingleng shareDataBase] createMovieTable];
           
           
           for (NSDictionary *dic in array) {
               Movie *movie = [[Movie alloc]init];
               [movie setValuesForKeysWithDictionary:dic];
               
               [[DataBaseSingleng shareDataBase]insertMovie:movie];
               [self.movieArray addObject:movie];
               [movie release];
               
           }
           
           //让菊花停止
           [self.hub hide:YES];
           
           //刷新页面一定要有数据之后再刷新
           [self.myTableView reloadData];

       }
       
   } error:^(NSError *error) {
       
       //让菊花停止
       [self.hub hide:YES];
       
   }];

}


#pragma mark -- 设置行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.movieArray.count;
}

#pragma mark -- 设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellaa = @"movie";
    MovieListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellaa];
    
    Movie *mov = [self.movieArray objectAtIndex:indexPath.row];
    cell.myMovie = mov;
    
    
    return cell;
}


#pragma mark -- 设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}


#pragma mar -- 触摸cell时触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //取消置灰
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MovieDetailViewController *detailVC = [[MovieDetailViewController alloc]init];
    
    NSLog(@"22222222%@",detailVC);
    //属性传值
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    detailVC.movieID = movie.movieId;
    detailVC.mov = movie;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
//    [detailVC release];
}

-(void)rightButton:(UIBarButtonItem *)button
{
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
